package com.PopulateDB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

import com.CMUBiS.util.Address;
import com.CMUBiS.util.Bid;

public class MySQLDBPopulator extends DBPopulator{
	private Connection conn = null;
	
	public MySQLDBPopulator(String fileName){
		super(fileName);
	}
	
	protected void initConnection(){
		try {
			Class.forName(this.getDriver()).newInstance();
			this.conn = DriverManager.getConnection(this.getURL(), this.getUsername(), this.getPassword());
			//return this.conn;
		}
		catch(Exception e){
			System.err.println("MySQLDBPopulator (initConnection): Could not open a new database connection to " + this.getURL());
			e.printStackTrace();
			//return null;
		}
	}
	
	public Connection getConnection(){
		if(this.conn != null){
			return this.conn;	
		}
		else{
			initConnection();
			
			//return initConnection();
		}
		
		return this.conn;
	}
	
	public void closeConnection(){
		if(this.conn != null){
				// close the connection
				try{
					this.conn.close();
				}
				catch(Exception e){
					System.err.println("MySQLDBPopulator (closeConnection): Could not close database connection");
					e.printStackTrace();
				}	
		}
	}
	
	public boolean dropAddUsers(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `users`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `users` (" +
				  "`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
				  "`firstname` varchar(20) DEFAULT NULL, " +
				  "`lastname` varchar(20) DEFAULT NULL, " +
				  "`username` varchar(20) NOT NULL, " +
				  "`password` varchar(20) NOT NULL, " +
				  "`authtoken` varchar(16) DEFAULT NULL, " +
				  "`email` varchar(50) NOT NULL, " +
				  "`rating` int(10) DEFAULT NULL, " +
				  "`creationDate` timestamp, " +
				  "PRIMARY KEY (`id`), " +
				  "UNIQUE KEY `usernamePasswordIndex` (`username`,`password`) " +
				  ") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddUsers): Could not delete the users");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddUsers): The database connection is not open");
		}
		
		return deleted;
	}
	
	public boolean dropAddAdresses(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `addresses`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `addresses` (" +
						"`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
						"`userID` int(10) unsigned NOT NULL, " +
						"`street` varchar(50) NOT NULL, " +
						"`town` varchar(50) NOT NULL, " +
						"`zip` varchar(10) NOT NULL, " +
						"`state` int(10) unsigned NOT NULL, " +
						"`isDefault` tinyint(1) NOT NULL DEFAULT '0', " +
						"PRIMARY KEY (`id`)" +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddAdresses): Could not delete the addresses");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddAdresses): The database connection is not open");
		}
		
		return deleted;
	}
	
	public int insertAddress(Address address){
		int addressID = -1;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the user
				PreparedStatement statement = conn.prepareStatement("INSERT INTO `addresses` (userID, street, town, zip, state, isDefault) "
						+ "VALUES (?, ?, ?, ?, ?, ?)");
				statement.setInt(1, address.getUserID());
				statement.setString(2, address.getStreet());
				statement.setString(3, address.getTown());
				statement.setString(4, address.getZip());
				statement.setInt(5, address.getState());
				statement.setBoolean(6, address.getIsDefault());

				int rows = statement.executeUpdate();
				
				// If there is a result, get the address ID number
				if(rows == 1){
					PreparedStatement getID = conn.prepareStatement("SELECT LAST_INSERT_ID()");
					ResultSet rs = getID.executeQuery();
					
					if(rs.next())
						addressID = rs.getInt(1);
				}

				statement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertAddress): Could not insert the address");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertAddress): The database connection is not open");
		}
		
		return addressID;
	}
	
	public int insertUser(String firstName, String lastName, String username, String password, String email, int rating, Date creationDate){
		int userID = -1;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the user
				PreparedStatement statement = conn.prepareStatement("INSERT INTO `users` (firstname, lastname, username, password, email, rating, creationDate) "
						+ "VALUES (?, ?, ?, ?, ?, ?, ?)");
				statement.setString(1, firstName);
				statement.setString(2, lastName);
				statement.setString(3, username);
				statement.setString(4, password);
				statement.setString(5, email);
				statement.setInt(6, rating);
				statement.setTimestamp(7, new java.sql.Timestamp(creationDate.getTime()));

				int rows = statement.executeUpdate();
				
				// If there is a result, get the user ID number
				if(rows == 1){
					PreparedStatement getID = conn.prepareStatement("SELECT LAST_INSERT_ID()");
					ResultSet rs = getID.executeQuery();
					
					if(rs.next())
						userID = rs.getInt(1);
				}

				statement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertUser): Could not insert the user");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertUser): The database connection is not open");
		}
		
		return userID;
	}
	
	public boolean dropAddItems(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `items`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `items` ( " +
						"`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
						"`name` text NOT NULL, " +
						"`description` text, " +
						"`startPrice` float unsigned NOT NULL, " +
						"`quantity` int(10) unsigned NOT NULL, " +
						"`reservePrice` float unsigned NOT NULL DEFAULT '0', " +
						"`buyNowPrice` float unsigned DEFAULT NULL, " +
						"`noOfBids` int(10) unsigned NOT NULL DEFAULT '0', " +
						"`currentBid` float unsigned NOT NULL DEFAULT '0', " +
						"`startDate` timestamp NOT NULL, " +
						"`endDate` timestamp NOT NULL, " +
						"`sellerID` int(10) unsigned NOT NULL, " +
						"`categoryID` int(10) unsigned NOT NULL, " +
						"`maxBid` float(10) unsigned NOT NULL DEFAULT '0', " +
						"`currentWinner` int(10) unsigned DEFAULT NULL, " +
						"`thumbnail` text, " +
						"PRIMARY KEY (`id`), " +
						"KEY `userIDIndex` (`sellerID`), " +
						"KEY `categoryIDIndex` (`categoryID`), " +
						"FULLTEXT KEY `textSearchIndex` (`name`,`description`) " +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddItems): Could not delete the items");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddItems): The database connection is not open");
		}
		
		return deleted;
	}
	
	public int insertItem(String name, String description, float startPrice, int quantity, float reservePrice, float buyNowPrice, Date startDate, Date endDate, int sellerID, int categoryID){
		int itemID = -1;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the item
				PreparedStatement statement = conn.prepareStatement("INSERT INTO `items` (name, description, startPrice, quantity, reservePrice, buyNowPrice, noOfBids, currentBid, startDate, endDate, sellerID, categoryID, maxBid) "
						+ "VALUES (?, ?, ?, ?, ?, ?, 0, 0, ?, ?, ?, ?, 0)");
				statement.setString(1, name);
				statement.setString(2, description);
				statement.setFloat(3, startPrice);
				statement.setInt(4, quantity);
				statement.setFloat(5, reservePrice);
				statement.setFloat(6, buyNowPrice);
				statement.setTimestamp(7, new java.sql.Timestamp(startDate.getTime()));
				statement.setTimestamp(8, new java.sql.Timestamp(endDate.getTime()));
				statement.setInt(9, sellerID);
				statement.setInt(10, categoryID);

				int rows = statement.executeUpdate();
				
				// If there is a result, get the item ID number
				if(rows == 1){
					PreparedStatement getID = conn.prepareStatement("SELECT LAST_INSERT_ID()");
					ResultSet rs = getID.executeQuery();
					
					if(rs.next())
						itemID = rs.getInt(1);
				}

				statement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertItem): Could not insert the item");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertItem): The database connection is not open");
		}
		
		return itemID;
	}
	
	public boolean dropAddBids(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `bids`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `bids` ( " +
						"`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
						"`userID` int(10) unsigned NOT NULL, " +
						"`itemID` int(10) unsigned NOT NULL, " +
						"`quantity` int(10) unsigned NOT NULL, " +
						"`bid` float unsigned NOT NULL, " +
						"`maxBid` float unsigned NOT NULL, " +
						"`bidDate` datetime NOT NULL, " +
						"PRIMARY KEY (`id`), " +
						"KEY `item` (`itemID`), " +
						"KEY `userIDIndex` (`userID`) " +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddBids): Could not delete the bids");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddBids): The database connection is not open");
		}
		
		return deleted;
	}
	
	public int insertBid(Bid bid, int itemID){
		int bidID = -1;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the item
				PreparedStatement insertBidStatement = conn.prepareStatement("INSERT INTO `bids` (userID, itemID, quantity, bid, maxBid, bidDate) "
						+ "VALUES (?, ?, ?, ?, ?, ?)");
				insertBidStatement.setInt(1, bid.getUserID());
				insertBidStatement.setInt(2, itemID);
				insertBidStatement.setInt(3, bid.getQuantity());
				insertBidStatement.setFloat(4, bid.getBid());
				insertBidStatement.setFloat(5, bid.getMaxBid());
				insertBidStatement.setTimestamp(6, new java.sql.Timestamp(bid.getBidDate().getTime()));

				int rows = insertBidStatement.executeUpdate();
				
				// If there is a result, get the item ID number
				if(rows == 1){
					PreparedStatement getID = conn.prepareStatement("SELECT LAST_INSERT_ID()");
					ResultSet rs = getID.executeQuery();
					
					if(rs.next())
						bidID = rs.getInt(1);
				}

				insertBidStatement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertBid): Could not insert the bid");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertBid): The database connection is not open");
		}
		
		return bidID;
	}
	
	public void updateItemMaxBid(int itemID, int noOfBids, float currentBid, float maxBid, int currentWinner){
		//int bidID = -1;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the item
				PreparedStatement updateItemStatement = conn.prepareStatement("UPDATE `items` SET `items`.`noOfBids` = ?, "
						+ "`items`.`currentBid` = ?, `items`.`maxBid` = ?, `items`.`currentWinner` = ? WHERE `items`.`id` = ?");
				updateItemStatement.setInt(1, noOfBids);
				updateItemStatement.setFloat(2, currentBid);
				updateItemStatement.setFloat(3, maxBid);
				updateItemStatement.setInt(4, currentWinner);
				updateItemStatement.setInt(5, itemID);

				int rows = updateItemStatement.executeUpdate();
				
				// If there is a result, get the item ID number
				/*if(rows == 1){
					PreparedStatement getID = conn.prepareStatement("SELECT LAST_INSERT_ID()");
					ResultSet rs = getID.executeQuery();
					
					if(rs.next())
						bidID = rs.getInt(1);
				}*/

				updateItemStatement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertBid): Could not insert the bid");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertBid): The database connection is not open");
		}
		
		//return bidID;
	}
	
	public boolean dropAddCategories(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `categories`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `categories` ( " +
						"`id` int(10) unsigned NOT NULL, " +
						"`name` varchar(50) NOT NULL, " +
						"`parent` int(10) unsigned DEFAULT '0', " +
						"PRIMARY KEY (`id`)" +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddCategories): Could not delete the categories");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddCategories): The database connection is not open");
		}
		
		return deleted;
	}
	
	public int insertCategory(int id, String name, int parent){
		int categoryID = -1;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the user
				PreparedStatement statement = conn.prepareStatement("INSERT INTO `categories` (id, name, parent) "
						+ "VALUES (?, ?, ?)");
				statement.setInt(1, id);
				statement.setString(2, name);
				statement.setInt(3, parent);

				int rows = statement.executeUpdate();
				
				// If there is a result, get the category ID number
				if(rows == 1){
					PreparedStatement getID = conn.prepareStatement("SELECT LAST_INSERT_ID()");
					ResultSet rs = getID.executeQuery();
					
					if(rs.next())
						categoryID = rs.getInt(1);
				}

				statement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertCategory): Could not insert the state");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertCategory): The database connection is not open");
		}
		
		return categoryID;
	}
	
	public boolean dropAddImages(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `images`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `images` ( " +
						"`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
						"`url` text NOT NULL, " +
						"`description` text, " +
						"PRIMARY KEY (`id`) " +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddImages): Could not delete the images");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddImages): The database connection is not open");
		}
		
		return deleted;
	}
	
	public boolean dropAddItemImage(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `item_image`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `item_image` ( " +
						"`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
						"`itemID` int(10) unsigned NOT NULL, " +
						"`imageID` int(10) unsigned NOT NULL, " +
						"`position` int(2) unsigned NOT NULL, " +
						"PRIMARY KEY (`id`), " +
						"KEY `imageIDIndex` (`itemID`) " +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddItemImage): Could not delete the item_images");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddItemImage): The database connection is not open");
		}
		
		return deleted;
	}
	
	public boolean dropAddPayments(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `payments`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `payments` ( " +
						"`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
						"`userID` int(10) unsigned NOT NULL, " +
						"`itemID` int(10) unsigned NOT NULL, " +
						"`quantity` int(10) unsigned NOT NULL, " +
						"`price` float unsigned NOT NULL, " +
						"`paidDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, " +
						"`street` varchar(50) NOT NULL, " +
						"`town` varchar(50) NOT NULL, " +
						"`zip` varchar(10) NOT NULL, " +
						"`state` varchar(20) NOT NULL, " +
						"`creditCardNo` varchar(16) NOT NULL, " +
						"`cvv` varchar(4) NOT NULL, " +
						"`expirationDate` datetime NOT NULL, " +
						"`nameOnCard` varchar(50) NOT NULL, " +
						"PRIMARY KEY (`id`) " +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddPayments): Could not delete the payments");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddPayments): The database connection is not open");
		}
		
		return deleted;
	}
	
	public boolean dropAddPurchased(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `purchased`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `purchased` ( " +
						"`id` int(10) unsigned NOT NULL AUTO_INCREMENT, " +
						"`userID` int(10) unsigned NOT NULL, " +
						"`itemID` int(10) unsigned NOT NULL, " +
						"`quantity` int(10) unsigned NOT NULL, " +
						"`price` float unsigned NOT NULL, " +
						"`purchaseDate` datetime NOT NULL, " +
						"`accountID` int(10) unsigned DEFAULT NULL, " +
						"`paid` tinyint(1) DEFAULT '0', " +
						"`paidDate` datetime DEFAULT NULL, " +
						"PRIMARY KEY (`id`) " +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddPurchased): Could not delete the purchased");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddPurchased): The database connection is not open");
		}
		
		return deleted;
	}
	
	public boolean dropAddStates(){
		boolean deleted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				PreparedStatement dropStatement = conn.prepareStatement("DROP TABLE IF EXISTS `states`");
				
				PreparedStatement createStatement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `states` ( " +
						"`id` int(11) NOT NULL AUTO_INCREMENT, " +
						"`shortName` varchar(2) NOT NULL, " +
						"`longName` varchar(35) NOT NULL, " +
						"PRIMARY KEY (`id`) " +
						") ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
				
				dropStatement.executeUpdate();
				createStatement.executeUpdate();
				
				deleted = true;
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (dropAddStates): Could not delete the states");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (dropAddStates): The database connection is not open");
		}
		
		return deleted;
	}
	
	public int insertState(String shortName, String longName){
		int stateID = -1;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the user
				PreparedStatement statement = conn.prepareStatement("INSERT INTO `states` (shortName, longName) "
						+ "VALUES (?, ?)");
				statement.setString(1, shortName);
				statement.setString(2, longName);

				int rows = statement.executeUpdate();
				
				// If there is a result, get the states ID number
				if(rows == 1){
					PreparedStatement getID = conn.prepareStatement("SELECT LAST_INSERT_ID()");
					ResultSet rs = getID.executeQuery();
					
					if(rs.next())
						stateID = rs.getInt(1);
				}

				statement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertState): Could not insert the state");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertState): The database connection is not open");
		}
		
		return stateID;
	}
	
	/*public boolean insertBid(int userID, int itemID, int quantity, int bid, int maxBid, Date bidDate){
		boolean inserted = false;
		Connection conn = this.getConnection();
		
		if(conn != null){
			try{
				// Create the SQL statement to insert the user
				PreparedStatement statement = conn.prepareStatement("INSERT INTO `bids` (userID, itemID, quantity, bid, maxBid, bidDate) "
						+ "VALUES (?, ?, ?, ?, ?, ?)");
				statement.setInt(1, userID);
				statement.setInt(2, itemID);
				statement.setInt(3, quantity);
				statement.setInt(4, bid);
				statement.setInt(5, maxBid);
				statement.setDate(6, new java.sql.Date(bidDate.getTime()));

				int rows = statement.executeUpdate();
				
				// If there is a result, set inserted to true
				if(rows == 1){
					inserted = true;
				}

				statement.close();
			}
			catch(Exception e){
				System.err.println("MySQLDBPopulator (insertBid): Could not insert the bid");
				e.printStackTrace();
			}
		}
		else{
			System.err.println("MySQLDBPopulator (insertBid): The database connection is not open");
		}
		
		return inserted;
	}*/
}
